package com.pointmi.controller;

import java.util.Date;
import java.util.Random;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.client.RestTemplate;

import com.pointmi.customeraccountdao.Customer;
import com.pointmi.customeraccountservice.CustomerAccountService;
import com.pointmi.model.CustomerModel;
import com.pointmi.model.LoginModel;
import com.pointmi.util.Pages;
import com.pointmi.util.Utility;

@Controller
public class AccountController {
	
	@Autowired
	RestTemplate restTemplate;
	
	@Autowired
	@Qualifier("customerService")
	private CustomerAccountService customerAccountService;
	
	public static final String REGISTER_VIEW_MODEL = "customer";
	public static final String LOGIN_VIEW_MODEL = "login";
	
//	@RequestMapping(value="/index.do")
//	public String home(Model model)
//	{
//	//	model.addAttribute("customer", new CustomerModel());
//		return "welcome";
//	}
	
//	@RequestMapping(value="/signup.do", method= RequestMethod.POST)
//	public String SaveCustomer(@Valid  @ModelAttribute("customer") CustomerModel customer, BindingResult result, Model model)
//	{
//		String returnPage = Pages.WELCOME.getValue();
//		if(!result.hasErrors())
//		{
//			Customer findCustomer = customerAccountService.findCustomerByEmail(customer.getemail());
//			if(findCustomer == null)
//			{
//				Customer newCustomer = save(customer);
//				customerAccountService.saveOrUpdate(newCustomer);
//				model.addAttribute("success", "Thank you for joining the Forte Oil loyalty scheme. " +
//						"An email containing default password and basic instructions has been sent to your email. ");
//				returnPage = Pages.REGISTERSUCCESS.getValue();
//			}
//			result.addError(new FieldError(REGISTER_VIEW_MODEL, "email", "This email has been taking. Please try another email."));
//		}
//		//Redisplay the registration for if there is error.
//		return returnPage;
//	}
//	
//	private Customer save(CustomerModel model)
//	{
//		Random number = new Random((new Date()).getTime());
//		Customer newCustomer = new Customer();
//		newCustomer.setCustomerId(""+number.nextInt());
//		newCustomer.setPassword("rabiu");
//		newCustomer.setEmail(model.getemail());
//		newCustomer.setFirstName(model.getFirstName());
//		newCustomer.setLastName(model.getlastname());
//		newCustomer.setHomeAddress(model.getaddress());
//		newCustomer.setCity(model.getcity());
//		newCustomer.setCountry("Nigeria");
//		newCustomer.setPhone(model.getphone());
//		newCustomer.setState(model.getstate());
//		newCustomer.setLastPasswordChangedDate(Utility.currentDate);
//		newCustomer.setCreatedDate(Utility.currentDate);
//		return newCustomer;
//	}
//	
//	@RequestMapping(value="/login.do")
//	public String login(Model model)
//	{
//		model.addAttribute("login", new LoginModel());
//		return "login";
//	}
//	
//	@RequestMapping(value="/login.do", method= RequestMethod.POST)
//	public String login(@Valid LoginModel login, BindingResult result, Model model)
//	{
//		if(result.hasErrors())
//		{
//			return "login";
//		}
//		Customer findCustomer = customerAccountService.findCustomerByEmailAndPassword
//				(login.getemail(), login.getpassword());
//		if(findCustomer != null)
//		{
//			model.addAttribute("user", findCustomer);
//			return "profile";
//		}
//		result.addError(new FieldError("login", "email", "Invalid login credentials. Please try again."));
//		return "login";
//	}
//	
//	@RequestMapping(value="/profile.do")
//	public String profile()
//	{
//		return "profile";
//	}
}
